'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactNative = require('react-native');

var _style = require('../style');

var _index = require('./style/index');

var _index2 = _interopRequireDefault(_index);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

var Tag = function (_React$Component) {
    (0, _inherits3['default'])(Tag, _React$Component);

    function Tag(props) {
        (0, _classCallCheck3['default'])(this, Tag);

        var _this = (0, _possibleConstructorReturn3['default'])(this, (Tag.__proto__ || Object.getPrototypeOf(Tag)).call(this, props));

        _this.onPress = function () {
            var _this$props = _this.props,
                disabled = _this$props.disabled,
                onChange = _this$props.onChange;

            if (disabled) {
                return;
            }
            var isSelect = _this.state.selected;
            _this.setState({
                selected: !isSelect
            }, function () {
                if (onChange) {
                    onChange(!isSelect);
                }
            });
        };
        _this.handleLongPress = function () {
            var _this$props2 = _this.props,
                disabled = _this$props2.disabled,
                onLongPress = _this$props2.onLongPress;

            if (disabled) {
                return;
            }
            if (onLongPress) {
                onLongPress();
            }
        };
        _this.onTagClose = function () {
            if (_this.props.onClose) {
                _this.props.onClose();
            }
            _this.setState({
                closed: true
            }, _this.props.afterClose);
        };
        _this.onPressIn = function (styles) {
            return function () {
                if (_this.closeDom) {
                    _this.closeDom.setNativeProps({
                        style: [styles.close, _reactNative.Platform.OS === 'ios' ? styles.closeIOS : styles.closeAndroid, {
                            backgroundColor: '#888'
                        }]
                    });
                }
            };
        };
        _this.onPressOut = function (styles) {
            return function () {
                if (_this.closeDom) {
                    _this.closeDom.setNativeProps({
                        style: [styles.close, _reactNative.Platform.OS === 'ios' ? styles.closeIOS : styles.closeAndroid]
                    });
                }
            };
        };
        _this.state = {
            selected: props.selected,
            closed: false
        };
        return _this;
    }

    (0, _createClass3['default'])(Tag, [{
        key: 'componentWillReceiveProps',
        value: function componentWillReceiveProps(nextProps) {
            if (this.props.selected !== nextProps.selected) {
                this.setState({
                    selected: nextProps.selected
                });
            }
        }
    }, {
        key: 'render',
        value: function render() {
            var _this2 = this;

            var _props = this.props,
                children = _props.children,
                disabled = _props.disabled,
                small = _props.small,
                closable = _props.closable,
                style = _props.style;

            var selected = this.state.selected;
            return _react2['default'].createElement(
                _style.WithTheme,
                { styles: this.props.styles, themeStyles: _index2['default'] },
                function (styles) {
                    var wrapStyle = void 0;
                    var textStyle = void 0;
                    if (!selected && !disabled) {
                        wrapStyle = styles.normalWrap;
                        textStyle = styles.normalText;
                    }
                    if (selected && !disabled) {
                        wrapStyle = styles.activeWrap;
                        textStyle = styles.activeText;
                    }
                    if (disabled) {
                        wrapStyle = styles.disabledWrap;
                        textStyle = styles.disabledText;
                    }
                    var sizeWrapStyle = small ? styles.wrapSmall : {};
                    var sizeTextStyle = small ? styles.textSmall : {};
                    var closableDom = closable && !small && !disabled ? _react2['default'].createElement(
                        _reactNative.TouchableWithoutFeedback,
                        { onPressIn: _this2.onPressIn(styles), onPressOut: _this2.onPressOut(styles), onPress: _this2.onTagClose },
                        _react2['default'].createElement(
                            _reactNative.View,
                            { ref: function ref(component) {
                                    return _this2.closeDom = component;
                                }, style: [styles.close, _reactNative.Platform.OS === 'ios' ? styles.closeIOS : styles.closeAndroid] },
                            _react2['default'].createElement(
                                _reactNative.Text,
                                { style: [styles.closeText, _reactNative.Platform.OS === 'android' ? styles.closeTransform : {}] },
                                '\xD7'
                            )
                        )
                    ) : null;
                    return !_this2.state.closed ? _react2['default'].createElement(
                        _reactNative.View,
                        { style: [styles.tag, style] },
                        _react2['default'].createElement(
                            _reactNative.TouchableWithoutFeedback,
                            { onPress: _this2.onPress, onLongPress: _this2.handleLongPress },
                            _react2['default'].createElement(
                                _reactNative.View,
                                { style: [styles.wrap, sizeWrapStyle, wrapStyle] },
                                _react2['default'].createElement(
                                    _reactNative.Text,
                                    { style: [styles.text, sizeTextStyle, textStyle] },
                                    children,
                                    ' '
                                )
                            )
                        ),
                        closableDom
                    ) : null;
                }
            );
        }
    }]);
    return Tag;
}(_react2['default'].Component);

exports['default'] = Tag;

Tag.defaultProps = {
    disabled: false,
    small: false,
    selected: false,
    closable: false,
    onClose: function onClose() {},
    afterClose: function afterClose() {},
    onChange: function onChange() {},
    onLongPress: function onLongPress() {}
};
module.exports = exports['default'];